package com.algo.generator.controller;

import com.algo.generator.common.BaseResponse;
import com.algo.generator.constant.ResponseCodeEnum;
import com.algo.generator.sequnse.SequnseCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IdGeneratorController {

    private static final Logger logger = LoggerFactory.getLogger(IdGeneratorController.class);

    /**
     * ID生成管理器
     */
    @Autowired
    @Qualifier("segmentLockSequnceCreator")
    private SequnseCreator sequnseCreator;

    /**
     * 使用 号段 + mysql主键的方式
     * @param stub 业务桩
     * @return 流水号
     */
    @GetMapping("/getSeq/{stub}")
    public BaseResponse generator(@PathVariable("stub") String stub) {
        if (stub == null || "".equals(stub)) {
            throw new IllegalArgumentException("stub is required");
        }
        Long seqId = sequnseCreator.create(stub);
        logger.info("获取到的seqId：{}", seqId);
        return new BaseResponse<>(ResponseCodeEnum.SUCCESS, seqId);
    }



}
